/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:44
 */

package com.lenovo.mso2o.ui.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lenovo.framework.base.ActionBarFragment;
import com.lenovo.framework.util.BitmapHelper;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.util.FileCacheUtil;
import com.lenovo.mso2o.view.widget.photoview.PhotoView;

import java.io.File;

import butterknife.Bind;


/**
 * Created by hippo on 12/20/15.
 */
public class ImageFragment extends ActionBarFragment{
    public static final String IS_REMOTE = "is_remote";
    public static final String PATH = "display_path";
    public static final String DELTED = "deleted";

    @Bind(R.id.photoview_openImageWebJS)
    PhotoView photoView;
    private File file;
    private boolean remote;
    //@Bind(R.id.loading_bar)
    //ProgressBar progressBar;
    @Bind(R.id.iv_gear_big_img_loading)
    ImageView bigGear;
    @Bind(R.id.iv_gear_small_img_loading)
    ImageView smallGear;

    @Bind(R.id.loading_layout_need_bg_img_loading)
    RelativeLayout layoutLoading;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        Bundle args = getArguments();
        remote = args.getBoolean(IS_REMOTE);
        String path = args.getString(PATH);
        file = new File(path);
        //progressBar.setVisibility(View.GONE);
        loadData(path, remote);
        return view;
    }

    private void startCartoon(){
        layoutLoading.setVisibility(View.VISIBLE);
        final Animation anim = AnimationUtils.loadAnimation(getActivity(), com.lenovo.framework.R.anim.myanim);
        LinearInterpolator lir = new LinearInterpolator();
        anim.setInterpolator(lir);
        final Animation anim2 = AnimationUtils.loadAnimation(getActivity(), com.lenovo.framework.R.anim.myanim_revert);
        anim2.setInterpolator(lir);
        bigGear.startAnimation(anim2);
        smallGear.startAnimation(anim);
    }

    private void stopCartoon(){
        layoutLoading.setVisibility(View.GONE);
        bigGear.clearAnimation();
        smallGear.clearAnimation();
    }

    @Override
    public void onPause() {
        stopCartoon();
        super.onPause();
    }

    private void loadData(String path, boolean remote){
        if(remote) {
            //progressBar.setVisibility(View.VISIBLE);
            startCartoon();
            FileCacheUtil.getFileFromWebOrLocal(path, new FileCacheUtil.FileLoadListener() {
                @Override
                public void onGotFile(File file) {
                    //Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    //if(photoView != null && progressBar != null) {
                    stopCartoon();
                    if(photoView != null) {
                        Glide.with(getContext()).load(file).thumbnail(0.4f).into(photoView);
                        //photoView.setImageBitmap(bitmap);
                        //progressBar.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onFail(String error) {
                    stopCartoon();
                    Toast.makeText(getActivity(), error, Toast.LENGTH_LONG).show();
                    //if(progressBar != null) {
                        //progressBar.setVisibility(View.GONE);
                        //Toast.makeText(getActivity(), error, Toast.LENGTH_LONG).show();
                    //}
                }

                @Override
                public void onProgress(int progress) {

                }
            }, getActivity());
        }else{
            Bitmap bitmap = BitmapHelper.safeDecodeScaledBitmapFromFileSystem(file.getAbsolutePath(), 600, 800);
            photoView.setImageBitmap(bitmap);
            //Glide.with(getContext()).load(file).into(photoView);
        }
    }

    @Override
    public int getActionBarRightResourceId() {
        return R.drawable.delete;
    }

    @Override
    public void onActionBarRightClick() {
        new AlertDialog.Builder(getContext()).setTitle("确认删除？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*
                        if (file.exists())
                            file.delete();
                            */
                        getArguments().putBoolean(DELTED, true);
                        getActivity().getIntent().putExtras(getArguments());
                        getActivity().setResult(Activity.RESULT_OK, getActivity().getIntent());
                        getActivity().finish();
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //点击不删除，不进行操作
            }
        }).show();
    }

    @Override
    public int provideContentRes() {
        return R.layout.image_display_fragment;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }
}
